﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Data;
using System.Text;
using App.Core.DB;

public partial class UserControls_SidebarAdvertisement : BaseUserControl
{
    private bool _loadLeftAdvertisements = false;
    public bool LoadLeftAdvertisements
    {
        get { return _loadLeftAdvertisements; }
        set { _loadLeftAdvertisements = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindAdvertisements();
    }
    //public override void DataBind()
    //{        
    //    base.DataBind();
    //}
    protected void BindAdvertisements()
    {        
        String position = this.LoadLeftAdvertisements ? "Left" : "Right";
        DbParameter[] parameters = new[] { new DbParameter("@Position", position) };   
        UtilityDAO dao =new UtilityDAO();
        DataSet ds = dao.GetDataSet(AppSQL.GET_ADVERTISEMENTS, parameters, false);
        if (!ds.IsEmpty() && ds.Tables[0].Rows.Count > 0)
        {   
            String html = @"
                    <div class='AdverstisementContainer'>
                        {0}<img class='AdverstisementImage' src='{1}/{2}' alt='' title='' />{3}
                    </div>
                    <div class='ListSeparator'><div></div></div>";

            String htmlForFlash = @"
                    <div class='AdverstisementContainer'>
                        {0}<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' 
                        codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'
                        width='200' height='auto'>
                        <param name='movie' value='{1}/{2}' />
                        <param name='quality' value='high' />
                        <embed src='{1}/{2}' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer'
                        type='application/x-shockwave-flash' width='200' height='auto'>
                        </embed>
                        </object>{3}
                    </div>
                    <div class='ListSeparator'><div></div></div>";

            StringBuilder sb = new StringBuilder(100);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                String linkedUrl = GetLinkedUrl(row);
                String fileType = NullHandler.GetString(row["FileType"]);
                sb.AppendFormat(String.Compare(fileType, "I", true) == 0 ? html : htmlForFlash
                    , linkedUrl
                    , AppConstants.ADVERTISEMENT_FILES
                    , NullHandler.GetString(row["FileName"])
                    , linkedUrl.IsNullOrEmpty() ? String.Empty : "</a>");
            }
            ltrAdvertisements.Text = sb.ToString();
        }
    }

    protected string GetLinkedUrl(DataRow row)
    {
        String url = NullHandler.GetString(row["LinkedUrl"]);
        if (url.IsNullOrEmpty())
            return String.Empty;
        else
            return String.Format("<a href='/Pages/Public/ShowAdd.aspx?{0}={1}' target='_blank'>", AppConstants.QueryString.ID, NullHandler.GetInt(row["ID"]));
    }
}